package com.mingqijia.gassafety.webserver.service.impl;

import cn.hutool.json.JSONUtil;
import com.gap.basic.base.CommonResponse;
import com.mingqijia.gassafety.db.entity.WorkOrderHistory;
import com.mingqijia.gassafety.db.entity.vo.UserServiceComboVO;
import com.mingqijia.gassafety.db.entity.vo.WorkOrderAndSmartKitVO;
import com.mingqijia.gassafety.shared.constant.ConsoleSkAlarmTypeEnum;
import com.mingqijia.gassafety.shared.constant.ConsoleSkDeviceTypeEnum;
import com.mingqijia.gassafety.webserver.feign.ConsoleSkFeignClient;
import com.mingqijia.gassafety.webserver.request.console.sk.ConsoleSkNotifyAlarmRequest;
import com.mingqijia.gassafety.webserver.response.ConsoleSkNotifyAlarmResponse;
import com.mingqijia.gassafety.webserver.service.ConsoleSkNotifyAlarmService;
import com.mingqijia.gassafety.webserver.service.FieldTemplateService;
import com.mingqijia.gassafety.webserver.service.ServiceComboDetailService;
import com.mingqijia.gassafety.webserver.listeners.OrderAutoGenProcess;
import com.mingqijia.gassafety.webserver.service.workorder.SkWorkOrderService;
import com.mingqijia.gassafety.webserver.service.workorder.WorkOrderHistoryService;
import com.mingqijia.gassafety.webserver.webSocket.WebSocketProcess;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author solyutian
 * @date 2023/6/27 11:26
 * 智慧厨房报警通知
 */
@Slf4j
@Service
public class ConsoleSkNotifyAlarmServiceImpl implements ConsoleSkNotifyAlarmService {


  @Autowired
  WebSocketProcess webSocketProcess;

  @Autowired
  OrderAutoGenProcess orderAutoGenProcess;

  @Autowired
  ConsoleSkFeignClient consoleSkFeignClient;

  @Autowired
  ServiceComboDetailService serviceComboDetailService;

  @Resource
  FieldTemplateService fieldTemplateService;

  @Resource
  WorkOrderHistoryService workOrderHistoryService;

  @Resource
  SkWorkOrderService skWorkOrderService;

  /**
   * 智慧厨房报警通知
   * @param request
   * @return
   */
  @Override
  public CommonResponse<Object> alarm(ConsoleSkNotifyAlarmRequest request) {
    log.info("智慧厨房报警通知:{}", JSONUtil.toJsonStr(request));
    if (ObjectUtils.isEmpty(request) || StringUtils.isEmpty(request.getId()) || StringUtils.isEmpty(request.getSpId())){
      return CommonResponse.failure("缺少必填参数");
    }
    //webSocket 给前端的参数
    ConsoleSkNotifyAlarmResponse response = new ConsoleSkNotifyAlarmResponse();
    BeanUtils.copyProperties(request,response);

    response.setNotifyName(ConsoleSkAlarmTypeEnum.getTypeByCode(request.getAlarmType()));
    response.setNotifyTime(request.getAlarmTime());
    response.setImei(request.getDeviceId());
    response.setInstallationPosition(request.getRoomName());
    response.setEquipmentTypeName(ConsoleSkDeviceTypeEnum.getTypeByCode(request.getDeviceType()));
    response.setPhone(request.getPhone());

    webSocketProcess.redisTopicSendMsg(request.getSpId(),JSONUtil.toJsonStr(response));

    if (1 == request.getPushType() && fieldTemplateService.isHuishengWorkSystemMode(request.getSpId()) && 31 != request.getAlarmType()) {//告警且开启自动工单
      List<String> ids = new ArrayList<>();
      ids.add(request.getId());
      List<WorkOrderAndSmartKitVO> workOrderAndSmartKitVOList = workOrderHistoryService.selectWorkOrderAndSmartKitByRecordId(ids);
      if (null == workOrderAndSmartKitVOList || workOrderAndSmartKitVOList.isEmpty()) {
        //服务项编码 1-sos报警器 2-漏水报警 3-烟雾报警 4-门锁报警
        String code = String.valueOf(request.getDeviceType());
        int userId = Integer.parseInt(request.getUserId());
        UserServiceComboVO userServiceComboVO = serviceComboDetailService.getUserServiceCombo(code, userId, request.getSpId());
        if (!ObjectUtils.isEmpty(userServiceComboVO)) {
          userServiceComboVO.setSkNotifyAlarmInfo(response);
          skWorkOrderService.autoCreateOrderByMsg(request.getSpId(), JSONUtil.toJsonStr(userServiceComboVO));
          //orderAutoGenProcess.redisTopicSendMsg(request.getSpId(), JSONUtil.toJsonStr(userServiceComboVO));
        }
      } else {
        log.info("该报警记录已经生成工单记录:{}", JSONUtil.toJsonStr(workOrderAndSmartKitVOList));
      }
    }

    return CommonResponse.success("");
  }
}
